<template>
  <div>
    <el-row :gutter="15">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <template slot="label">
                手机号
                <el-tooltip class="item" effect="dark"
                            content="手机号将会作为“基本信息”中联系人号码使用，请确保您的手机号将会用作注册信息"
                            placement="top-start">
                  <el-icon class="el-icon-warning"/>
                </el-tooltip>
              </template>

              <el-input v-model="formData.phone" prefix-icon="el-icon-mobile-phone" placeholder="请输入手机号"
                        clearable>
                <el-select v-model="formData.phoneType" slot="prepend" placeholder="请选择" style="width: 170px">
                  <el-option :label="item.label" :value="item.value" v-for="item in phoneSource"></el-option>
                </el-select>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="2" :offset="1">
            <el-button style="width: 100%" @click="getPhoneCode" :disabled="verification.tim">
              {{ verification.tim?verification.tim:'发送验证码' }}
            </el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="15">
            <el-form-item label="验证码" prop="code">
              <el-input v-model.number="formData.code"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item size="large">
              <el-button type="primary" @click="submitForm">验证</el-button>
              <el-button @click="resetForm">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
  </div>
</template>
<script>

import {verificationCode, verificationSendCode} from "@/api/wechat";
import {Message} from "element-ui";

export default {
  components: {},
  props: [],
  data() {
    return {
      phoneSource: [
        {
          label: '中国大陆（+86）',
          value: '1'
        }
      ],
      verification: {
        tim: undefined,
        interval: undefined
      },
      formData: {
        phone: undefined,
        code: undefined,
        phoneType: '1',
      },
      rules: {
        code: [{
          required: true,
          message: '请输入验证码',
          trigger: 'blur'
        }, {type: 'number', message: '非法验证码', trigger: 'blur'}],
        phone: [{
          required: true,
          message: '请输入手机号',
          trigger: 'blur'
        }, {
          pattern: /1[3456789]\d{9}/g,
          message: '请输入正确手机号',
          trigger: 'blur'
        }],
      },
    }
  },
  computed: {},
  watch: {},
  created() {
  },
  mounted() {
  },
  methods: {
    getPhoneCode() {
      this.$refs.elForm.validateField('phone', valid => {
        if (!valid) {
          this.verification.tim = 60
          //send({phone: this.registerForm.phonenumber})
          // 发送验证码
          verificationSendCode({
            phone: this.formData.phone
          })
          this.verification.interval = setInterval(() => {
            this.verification.tim = this.verification.tim - 1
            if (this.verification.tim <= 0) {
              clearInterval(this.verification.interval)
              this.verification.tim = undefined
            }
          }, 1000)
        }
      })

    },
    submitForm() {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        // TODO 提交表单
        verificationCode({
          phone: this.formData.phone,
          verificationCode: this.formData.code
        }).then(res => {
          if (res.code == 200) {
            this.$emit('verification', this.formData)
          } else {
            Message.warning(res.msg)
          }
        }).catch(err => {
          Message.error(res.msg)
        })
            //this.$emit('verification', this.formData)
      })
    },
    resetForm() {
      this.$refs['elForm'].resetFields()
    },
  }
}

</script>
<style>
</style>
